﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CookComputing.XmlRpc;
using CookComputing.Blogger;
using GestionNewsLetters.Controllers;
using GestionNewsLetters.Models.Classes;
using System.Web.Mvc;
using GestionNewsLetters.Models.Dao;

namespace GestionNewsLetters.WebService
{
    /// <summary>
    /// Summary description for MetaWeblog
    /// </summary>
    public class MetaWeblog : XmlRpcService, IBlogger
    {

        public bool deletePost(string appKey, string postid, string username, string password, bool publish)
        {
            throw new NotImplementedException();
        }

        public object editPost(string postid, string username, string password, Post content, bool publish)
        {
            IMembershipService membership = new AccountMembershipService();

            //On authentifie l'utilisateur
            if (membership.ValidateUser(username, password))
            {
                NewsletterDao newsletter = new NewsletterDao(Context.Server, Helper.GetUrlApplication());
                newsletter.Update(content, int.Parse(postid));

                return true;
            }
            throw new Exception("Identifiants invalides");
        }

        public Category[] getCategories(string blogid, string username, string password)
        {
            throw new NotImplementedException();
        }

        public Post getPost(string appKey, string postid, string username, string password)
        {
            throw new NotImplementedException();
        }

        public Post[] getRecentPosts(string appKey, string blogid, string username, string password, int numberOfPosts)
        {
            throw new NotImplementedException();
        }

        public string getTemplate(string appKey, string blogid, string username, string password, string templateType)
        {
            throw new NotImplementedException();
        }

        public UserInfo getUserInfo(string appKey, string username, string password)
        {
            IMembershipService membership = new AccountMembershipService();

            //On authentifie l'utilisateur
            if (membership.ValidateUser(username, password))
            {
                UserInfo userInfo = new UserInfo();
                userInfo.nickname = username;
                userInfo.url = "";
                return userInfo;
            }
            throw new Exception("Identifiants invalides");
        }

        public BlogInfo[] getUsersBlogs(string appKey, string username, string password)
        {
            IMembershipService membership = new AccountMembershipService();

            //On authentifie l'utilisateur
            if (membership.ValidateUser(username, password))
            {
                BlogInfo[] infos = new BlogInfo[1];
                infos[0] = new BlogInfo();
                infos[0].blogName = "Newsletters du Laboratoire SUPINFO des technologies Mircosoft";
                infos[0].blogid = "Newsletters";
                infos[0].url = System.Configuration.ConfigurationManager.AppSettings["LiveWriter_redirectUrl"];
                return infos;

            }
            throw new Exception("Identifiants invalides");
        }

        public string newPost(string blogid, string username, string password, Post content, bool publish)
        {
            IMembershipService membership = new AccountMembershipService();
            
            //On authentifie l'utilisateur
            if (membership.ValidateUser(username, password))
            {
                NewsletterDao newsletter = new NewsletterDao(Context.Server, Helper.GetUrlApplication());
                return newsletter.Create(content).ToString();

            }
            throw new Exception("Identifiants invalides");
        }

        public bool setTemplate(string appKey, string blogid, string username, string password, string template, string templateType)
        {
            throw new NotImplementedException();
        }
    }
}